package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.UserService;
import com.yunfeisoft.business.model.Room;
import com.yunfeisoft.business.model.RoomDisinfectionRecords;
import com.yunfeisoft.business.service.inter.RoomDisinfectionRecordsService;
import com.yunfeisoft.business.service.inter.RoomService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: RoomDisinfectionRecordsController
 * Description: 房间周卫生和消毒记录表Controller
 * Author: Jackie liu
 * Date: 2020-05-22
 */
@Controller
public class RoomDisinfectionRecordsController extends BaseController {

    @Autowired
    private RoomDisinfectionRecordsService roomDisinfectionRecordsService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    /**
     * 添加房间周卫生和消毒记录表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/roomDisinfectionRecords/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(RoomDisinfectionRecords record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "roomId", "房号为空");
        validator.required(request, "roomHygieneStatus", "房间卫生状态为空");
        validator.required(request, "roomBathroomStatus", "房间卫生间状态为空");
        validator.required(request, "roomGroundStatus", "房间地面状态为空");
        validator.required(request, "towelDisinfectionStatus", "毛巾消毒为空");
        validator.required(request, "cupwareDisinfectionStatus", "口杯餐具消毒为空");
        validator.required(request, "washbasinDisinfectionStatus", "脸盆脚盆消毒");
        validator.required(request, "openWindowStatus", "开窗通风为空");
        validator.required(request, "glassCleanStatus", "玻璃清洁为空");
        validator.required(request, "bedChangeStatus", "床单换洗为空");
        validator.required(request, "ragMopStatus", "抹布拖把为空");
        validator.required(request, "toiletDisinfectionStatus", "卫生间消毒为空");
        validator.required(request, "wheelchairStatus", "轮椅状态为空");
        validator.required(request, "corridorStatus", "走廊状态为空");
        validator.required(request, "guardrailStatus", "护栏状态为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        roomDisinfectionRecordsService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改房间周卫生和消毒记录表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/roomDisinfectionRecords/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(RoomDisinfectionRecords record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "roomId", "房号为空");
        validator.required(request, "roomHygieneStatus", "房间卫生状态为空");
        validator.required(request, "roomBathroomStatus", "房间卫生间状态为空");
        validator.required(request, "roomGroundStatus", "房间地面状态为空");
        validator.required(request, "towelDisinfectionStatus", "毛巾消毒为空");
        validator.required(request, "cupwareDisinfectionStatus", "口杯餐具消毒为空");
        validator.required(request, "washbasinDisinfectionStatus", "脸盆脚盆消毒");
        validator.required(request, "openWindowStatus", "开窗通风为空");
        validator.required(request, "glassCleanStatus", "玻璃清洁为空");
        validator.required(request, "bedChangeStatus", "床单换洗为空");
        validator.required(request, "ragMopStatus", "抹布拖把为空");
        validator.required(request, "toiletDisinfectionStatus", "卫生间消毒为空");
        validator.required(request, "wheelchairStatus", "轮椅状态为空");
        validator.required(request, "corridorStatus", "走廊状态为空");
        validator.required(request, "guardrailStatus", "护栏状态为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        roomDisinfectionRecordsService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询房间周卫生和消毒记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/roomDisinfectionRecords/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        RoomDisinfectionRecords record = roomDisinfectionRecordsService.load(id);
        if (!ObjectUtils.isEmpty(record)) {
            Room room = roomService.load(record.getRoomId());
            record.setFloorNum(room.getFloorNum());
            record.setRoomNum(room.getRoomNum());
            User user = userService.load(record.getCreateId());
            record.setCreateName(user.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询房间周卫生和消毒记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/roomDisinfectionRecords/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String floorNum = ServletRequestUtils.getStringParameter(request, "floorNum", null);
        String roomNum = ServletRequestUtils.getStringParameter(request, "roomNum", null);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        if (!StringUtils.isBlank(floorNum)) {
            params.put("floorNum", floorNum);
        }
        if (!StringUtils.isBlank(roomNum)) {
            params.put("roomNum", roomNum);
        }
        Page<RoomDisinfectionRecords> page = roomDisinfectionRecordsService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除房间周卫生和消毒记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/roomDisinfectionRecords/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        roomDisinfectionRecordsService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
